package com.ls.rabbitmqService.utils;

import com.alibaba.fastjson.JSONObject;
import com.ls.baseService.entity.JpushParameterConfig;
import com.ls.baseService.entity.RabbitmqConfig;
import com.ls.baseService.utils.Constants;
import com.ls.jpushMessageClient.JPushUtils;
import com.ls.rabbitmqService.RabbitmqConnectionPool;
import com.ls.rabbitmqService.RabbitmqConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ConfigService {
	private static Logger LOG = LoggerFactory.getLogger(ConfigService.class);

    private final static Properties props = new Properties();

    private static long configLastModifyTime = 0;

    static ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    static {
        loadConfig();
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                loadConfig();
            }
        },0,2,TimeUnit.SECONDS);
    }

    public static void initConfig(){
        LOG.info("====ConfigService initConfig");
    }

    private static void loadConfig() {
        BufferedInputStream bufferedInputStream=null;
        //写入文件
        String property = System.getProperty("user.dir");
        File file=new File(property+File.separator+"config.json");
        try {
            //文件不存在直接返回
            if(!file.exists()){
                return;
            }
            if(file.lastModified()==configLastModifyTime){
                return;
            }
            configLastModifyTime=file.lastModified();
            bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
            int size=1024;
            byte[] bytes=new byte[size];
            StringBuilder stringBuilder=new StringBuilder();
            int read=0;
            while((read = bufferedInputStream.read(bytes,0,size))>0){
                stringBuilder.append(new java.lang.String(bytes,0,read));
            }
            String config = stringBuilder.toString();
            loadConstantInfo(config);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(bufferedInputStream!=null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void loadConstantInfo(String config){
        LOG.info("====loadConstantInfo:"+config);
        JSONObject jsonObject = JSONObject.parseObject(config);
        RabbitmqConsumer.pushConfig=jsonObject;
        JPushUtils.loadAreaConfig(jsonObject);
        RabbitmqConfig rabbitmqConfig = JSONObject.parseObject(jsonObject.getString("rabbitmqConfig"), RabbitmqConfig.class);
        JpushParameterConfig jpushConfig = JSONObject.parseObject(jsonObject.getString("jpushConfig"), JpushParameterConfig.class);
        Constants.rabbitmqConfig=rabbitmqConfig;
        Constants.jpushParameterConfig=jpushConfig;
        //加载rabbitmq pool
        RabbitmqConnectionPool.initConnectionFactory(rabbitmqConfig.getUsername(),rabbitmqConfig.getPassword(),
                rabbitmqConfig.getVirtualHost(),rabbitmqConfig.getHost(),rabbitmqConfig.getPort());
        RabbitmqConsumer.loadQueue(jsonObject);
    }
    
}